
//
//  DoorSystemDeviceInfoViewController.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/24.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorSystemDeviceInfoViewController.h"
#import "DSDeviceInfoInputTableViewCell.h"
@interface DoorSystemDeviceInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
/** tableView **/
@property(nonatomic,strong) UITableView *tableView;

/** deviceName **/
@property(nonatomic,copy) NSString *deviceName;

/** model **/
@property(nonatomic,strong) MyCamera *camera;
@end

@implementation DoorSystemDeviceInfoViewController



- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setNavigationStyle];
    self.camera = [AppDelegate globalDelegate].cameraModel;
    
    self.deviceName = self.camera.name;
}


#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DSDeviceInfoInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DSDeviceInfoInputTableViewCell"];
    switch (indexPath.row) {
        case 0:
        {
            cell.tipLabel.text = @"名称";
            cell.contentLabel.hidden = YES;
            cell.inputTextField.returnKeyType = UIReturnKeyDone;
            cell.inputTextField.text = self.camera.name;
            
            cell.textFielfEnd_block = ^(NSString *text){
              
                self.deviceName = text;
            };
            break;
        }
        case 1:
        {
            cell.tipLabel.text = @"UID";
            cell.inputTextField.hidden = YES;
            cell.contentLabel.text = [AppDelegate globalDelegate].cameraModel.uid;

            break;
        }
        case 2:
        {
            cell.tipLabel.text = @"设备密码";
            cell.contentLabel.hidden = YES;
            [cell.inputTextField setSecureTextEntry:YES];
            cell.inputTextField.returnKeyType = UIReturnKeyDone;
            cell.inputTextField.enabled = NO;
            cell.inputTextField.text = self.camera.viewPwd;

            cell.textFielfEnd_block = ^(NSString *text){
                
                
            };
            break;
        }
        default:
            break;
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60.f;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"DSDeviceInfoInputTableViewCell" bundle:nil] forCellReuseIdentifier:@"DSDeviceInfoInputTableViewCell"];
    }
    
    return _tableView;
    
}


- (void)setNavigationStyle{
    
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setNavigationTitle:@"设备信息" titleColor:[UIColor colorWithRed:210/255.0 green:177/255.0 blue:104/255.0 alpha:1]];
    [self setRightButtonTitle:@"保存"];

}


- (void)onRightButtonClick:(id)sender{
    
    NSLog(@"数据库保存");
    [self.view endEditing:YES];
    if (self.deviceName  == nil || [self.deviceName  length] == 0) {
        
        return [SVProgressHUD showErrorWithStatus:@"请输入需要修改的设备名称"];
        
    }

    if ([self.deviceName length]<=6 && [self.deviceName length]>0){
        
        if ([AppDelegate globalDelegate].database != NULL) {
            
            BOOL success = [[AppDelegate globalDelegate].database executeUpdate:@"UPDATE device SET dev_nickname=?, view_pwd=? WHERE dev_uid=?", self.deviceName, self.camera.viewPwd, self.camera.uid];
            
            if (success == YES) {
                
                [AppDelegate globalDelegate].cameraModel.name = self.deviceName;
                [AppDelegate globalDelegate].cameraModel.nickname = self.deviceName;
                
                [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:@"修改失败"];
                NSLog(@"Fail to update device to database.");
            }
            
        }
        
    }else{
        
        [SVProgressHUD showErrorWithStatus:@"输入名称过长或输入了非法字符"];
        
    }

    
   
    
    

    
    
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(superView);
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
